﻿using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using AEM.Utils;

namespace AEM.Data.DAL
{
    public static class StatusPoints
    {
        public static bool Add(Model.StatusPoints model)
        {
            //TableStatusPoints
            string sql = @"INSERT INTO TableStatusPoints
                (StatusByte, StatusBit, LiftStatusNum)
                VALUES   (@StatusByte,@StatusBit,@LiftStatusNum)";

            int ret = AccessHelper.ExecuteNonQuery(sql, Param(model));
            if (ret > 0) return true;
            return false;
        }

        public static bool Update(Model.StatusPoints model)
        {
            string sql = @"UPDATE [TableStatusPoints] SET 
                        StatusByte=@StatusByte, StatusBit=@StatusBit, LiftStatusNum=@LiftStatusNum
                        WHERE ([ID]=@ID)";

            int ret = AccessHelper.ExecuteNonQuery(sql, Param(model));
            if (ret > 0) return true;
            return false;
        }

        private static OleDbParameter[] Param(Model.StatusPoints model)
        {
            List<OleDbParameter> param = new List<OleDbParameter>();
            param.Add(new OleDbParameter("@StatusByte", model.StatusByte));
            param.Add(new OleDbParameter("@StatusBit", model.StatusBit));
            param.Add(new OleDbParameter("@LiftStatusNum", model.LiftStatusNum));
            param.Add(new OleDbParameter("@ID", model.ID));
            return param.ToArray();
        }

        public static Model.StatusPoints Select(string condition)
        {
            DataTable dt = Global.List("TableStatusPoints", condition);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt.Rows[0]);
            }
            return null;
        }

        private static Model.StatusPoints Deserialize(DataRow dr)
        {
            Model.StatusPoints model = null;
            if (null != dr)
            {
                model = new Model.StatusPoints();
                model.ID = dr["ID"].ToString().toInt32();
                model.StatusByte = dr["StatusByte"].ToString();
                model.StatusBit = dr["StatusBit"].ToString();
                model.LiftStatusNum = dr["LiftStatusNum"].ToString().toInt32();
            }
            return model;
        }

        public static bool Delete(int id)
        {
            return Global.Delete("TableStatusPoints", "ID", id.ToString());
        }

        public static List<Model.StatusPoints> List(string condition)
        {
            DataTable dt = Global.List("TableStatusPoints", condition);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt);
            }
            return null;
        }

        private static List<Model.StatusPoints> Deserialize(DataTable dt)
        {
            List<Model.StatusPoints> list = null;
            if (null != dt && dt.Rows.Count > 0)
            {
                list = new List<Model.StatusPoints>();
                foreach (DataRow r in dt.Rows)
                {
                    list.Add(Deserialize(r));
                }
            }
            return list;
        }

    }
}
